package com.mmall.controller.backend;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICategoryService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by root on 18-4-7.
 */
@Controller
@RequestMapping("/manage/category/")
public class CategoryManageController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ICategoryService categoryService;

    @RequestMapping("add_category.do")
    @ResponseBody
    public ServerResponse  addCategory(HttpSession session, String categoryName, @RequestParam(value = "parentId",defaultValue = "0") Integer parentId){
        ServerResponse resp = UserHelper.validAdmin(session);
        if (!resp.isSuccess()){
            return resp;
        }
       return categoryService.addCategory(categoryName, parentId);
    }


    @RequestMapping("set_category_name.do")
    @ResponseBody
    public ServerResponse setCategoryName(HttpSession session, Integer categoryId, String categoryName){
        ServerResponse resp = UserHelper.validAdmin(session);
        if (!resp.isSuccess()){
            return resp;
        }
        return categoryService.setCategoryName(categoryName, categoryId);
    }

    @RequestMapping("get_category.do")
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId){
        ServerResponse resp = UserHelper.validAdmin(session);
        if (!resp.isSuccess()){
            return resp;
        }
        return categoryService.getChildrenParallelCategory(categoryId);
    }


    @RequestMapping("get_deep_category.do")
    @ResponseBody
    public ServerResponse getDeepCategory(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId){
        ServerResponse resp = UserHelper.validAdmin(session);
        if (!resp.isSuccess()){
            return resp;
        }
        return categoryService.getDeepCategory(categoryId);
    }

}
